package com.itgaohe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itgaohe.dao.entity.Shop;
import com.itgaohe.dao.mapper.ShopDao;
import com.itgaohe.dto.resp.CursorResponseDTO;
import com.itgaohe.service.ShopService;
import com.itgaohe.utils.CreateCursorUtil;
import com.itgaohe.utils.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class ShopServiceImpl extends ServiceImpl<ShopDao, Shop> implements ShopService {
    private final ShopDao shopDao;

    @Override
    public Shop queryShopById(Long shopId) {
        return this.getById(shopId);
    }

    /**
     * 根据类型ID分页查询商铺
     */
    @Override
    public Result queryShopByType(Long typeId, Long cursor) {
        // 初始化游标（首次请求）
        if (cursor == 1) {
            cursor = CreateCursorUtil.initCursor();
        }

        // 创建响应对象
        CursorResponseDTO<Shop> response = new CursorResponseDTO<>();
        response.setHasMore(true);

        // 查询商铺数据
        List<Shop> shops = shopDao.selectShopByType(typeId, cursor);

        // 处理分页信息
        if (!shops.isEmpty()) {
            // 获取最后一个商铺的游标作为下一页的游标
            Long nextCursor = shops.get(shops.size() - 1).getCursorIndex();
            response.setNextCursor(nextCursor);

            // 检查是否还有更多数据
            if (shops.size() < 7) {
                response.setHasMore(false);
            }
        } else {
            // 无数据时设置默认值
            response.setHasMore(false);
            response.setNextCursor(0L);
        }

        response.setData(shops);
        return Result.ok(response);
    }

    //获取商铺详细信息
    @Override
    public Shop getShopById(Long shopId) {
        return this.getById(shopId);
    }


    @Override
    public Result queryAllShops(Long cursor) {
        /**
         * 分页查询所有商铺
         */

            // 初始化游标（首次请求）
            if (cursor == 1) {
                cursor = CreateCursorUtil.initCursor();
            }

            // 创建响应对象
            CursorResponseDTO<Shop> response = new CursorResponseDTO<>();
            response.setHasMore(true);

            // 查询商铺数据
            List<Shop> shops = shopDao.queryAllShops(cursor);

            // 处理分页信息
            if (!shops.isEmpty()) {
                // 获取最后一个商铺的游标作为下一页的游标
                Long nextCursor = shops.get(shops.size() - 1).getCursorIndex();
                response.setNextCursor(nextCursor);

                // 检查是否还有更多数据
                if (shops.size() < 7) {
                    response.setHasMore(false);
                }
            } else {
                // 无数据时设置默认值
                response.setHasMore(false);
                response.setNextCursor(0L);
            }

            response.setData(shops);
            return Result.ok(response);

    }


}
